package org.springblade.oa.config;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LK
 * @version 1.0.0
 * @Title: DynamicDataSourceContextHolder.java
 * @Description: TODO
 * @date 2020/5/20 11:09
 */
@Slf4j
public class DynamicDataSourceContextHolder {
	/**
	 * 存储已经注册的数据源的key
	 */
	public static List<String> dataSourceIds = new ArrayList<>();

	/**
	 * 线程级别的私有变量
	 */
	private static final ThreadLocal<String> HOLDER = new ThreadLocal<>();

	public static String getDataSourceRouterKey() {
		return HOLDER.get();
	}

	public static void setDataSourceRouterKey(String dataSourceRouterKey) {
		log.info("切换至{}数据源", dataSourceRouterKey);
		HOLDER.set(dataSourceRouterKey);
	}

	/**
	 * 设置数据源之前一定要先移除
	 */
	public static void removeDataSourceRouterKey() {
		HOLDER.remove();
	}

	/**
	 * 判断指定DataSrouce当前是否存在
	 *
	 * @param dataSourceId
	 * @return
	 */
	public static boolean containsDataSource(String dataSourceId) {
		return dataSourceIds.contains(dataSourceId);
	}
}
